package cn.wolfcode.trip.base.service.impl;
import cn.wolfcode.trip.base.domain.Cart;
import cn.wolfcode.trip.base.domain.OrderItem;
import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.mapper.CartMapper;
import cn.wolfcode.trip.base.mapper.OrderItemMapper;
import cn.wolfcode.trip.base.mapper.ProductMapper;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.service.IOrderItemService;
import cn.wolfcode.trip.base.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author by mycp
 * @Classname OrderItemServiceImpl
 * @Description TODO
 * @Date 2018/11/16 17:18
 */
@Service
public class OrderItemServiceImpl implements IOrderItemService {
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private SignMapper signMapper;

    @Override
    public void saveOrder(OrderItem orderItem) {
        // 查询出对应的商品库存, 判断库存是否足够
        Product product = productMapper.selectByPrimaryKey(orderItem.getProductId());
        if (product.getInventory() == 0){
            throw new RuntimeException("库存不足");
        }

        // 生成订单号
        String orderNumber = UUIDUtils.getUUID();
        orderItem.setOrderNumber(orderNumber);
        // 设置下单时间
        orderItem.setOrderTime(new Date());
        // 设置订单状态为未付款状态
        orderItem.setStatus(1L);
        // 保存订单信息
        orderItemMapper.insert(orderItem);

        // 减掉对应的库存, 增添对应的兑换量
        product.setInventory(product.getInventory() - 1);
        product.setConversionNum(product.getConversionNum() + 1);

        // 更新库存商品信息
        productMapper.updateByPrimaryKey(product);


        // 删除对应的购物车信息
        Cart cart = new Cart();
        cart.setProductId(orderItem.getProductId());
        cart.setUserId(orderItem.getUserId());
        cartMapper.deleteCartByUserIdAndProductId(cart);
    }

    @Override
    public List<OrderItem> getOrders(Long userId, Long status) {
        return orderItemMapper.selectOrderByUserIdAndStatus(userId, status);
    }

    @Override
    public void payOrder(Long orderId) {
        // 查询出订单信息
        OrderItem orderItem = orderItemMapper.selectByPrimaryKey(orderId);

        // 查询用户的积分
        Sign sign = signMapper.getSignByUserId(orderItem.getUserId());

        // 查询出支付订单所需积分
        Long signPoints = sign.getSignPoints();
        // 用户积分扣减
        signPoints = signPoints - orderItem.getPoints();
        sign.setSignPoints(signPoints);
        // 更新用户积分
        signMapper.updateByPrimaryKey(sign);

        // 更新用户兑换商品订单 , 已支付状态
        orderItem.setStatus(2L);
        orderItemMapper.updateByPrimaryKey(orderItem);
    }

    @Override
    public OrderItem getOrderByOrderId(Long orderId) {
        return orderItemMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public void cancelOrder(Long orderId) {
        orderItemMapper.cancelOrder(orderId);
    }
}
